#!/usr/bin/env python
# -*- coding:utf-8 -*-

import datetime
import math
import json

from sqlalchemy import or_

from app import db
from ..models.adminModel import Admin
from ..utils import commons
from ..utils.response_code import RET, error_map_EN
from ..utils.loggings import loggings
from ..utils.rsa_encryption_decryption import RSAEncryptionDecryption


class AdminController(Admin):

    # add
    @classmethod
    def add(cls, **kwargs):
        from ..utils.generate_id import GenerateID
        adminID = GenerateID.create_random_id()
        
        try:
            model = Admin(
                adminID=adminID,
                userID=kwargs.get('userID'),
                departmentID=kwargs.get('departmentID'),
                name=kwargs.get('name'),
                email=RSAEncryptionDecryption.encrypt(kwargs.get('email')) if kwargs.get('email') else None,
                phone=RSAEncryptionDecryption.encrypt(kwargs.get('phone')) if kwargs.get('phone') else None,
                officeAddress=kwargs.get('officeAddress'),
                addTime=kwargs.get('addTime'),
                
            )
            db.session.add(model)
            db.session.commit()
            results = {
                'add_time': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                'adminID': model.adminID,
                
            }
            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'data': results}
            
        except Exception as e:
            db.session.rollback()
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'data': {'error': str(e)}}
        finally:
            db.session.close()

    # get
    @classmethod
    def get(cls, **kwargs):
        try:
            filter_list = [cls.isDelete == 0]
            if kwargs.get('adminID'):
                filter_list.append(cls.adminID == kwargs['adminID'])
            else:
                if kwargs.get('userID'):
                    filter_list.append(cls.userID == kwargs.get('userID'))
                if kwargs.get('departmentID') is not None:
                    filter_list.append(cls.departmentID == kwargs.get('departmentID'))
                if kwargs.get('name'):
                    filter_list.append(cls.name == kwargs.get('name'))
                if kwargs.get('officeAddress'):
                    filter_list.append(cls.officeAddress == kwargs.get('officeAddress'))
                if kwargs.get('addTime'):
                    filter_list.append(cls.addTime == kwargs.get('addTime'))
                

            page = int(kwargs.get('Page', 1))
            size = int(kwargs.get('Size', 10))
            
            admin_info = db.session.query(cls).filter(*filter_list)
            
            count = admin_info.count()
            pages = math.ceil(count / size)
            admin_info = admin_info.limit(size).offset((page - 1) * size).all()
   
            results = commons.query_to_dict(admin_info)
            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'totalCount': count, 'totalPage': pages, 'data': results}
            
        except Exception as e:
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'data': {'error': str(e)}}
        finally:
            db.session.close()

    # delete
    @classmethod
    def delete(cls, **kwargs):
        try:
            filter_list = [cls.isDelete == 0]
            if kwargs.get('adminID'):
                primary_key_list = []
                for primary_key in str(kwargs.get('adminID')).replace(' ', '').split(','):
                    primary_key_list.append(cls.adminID == primary_key)
                filter_list.append(or_(*primary_key_list))
                
            else:
                if kwargs.get('userID'):
                    filter_list.append(cls.userID == kwargs.get('userID'))
                if kwargs.get('departmentID') is not None:
                    filter_list.append(cls.departmentID == kwargs.get('departmentID'))
                if kwargs.get('name'):
                    filter_list.append(cls.name == kwargs.get('name'))
                if kwargs.get('officeAddress'):
                    filter_list.append(cls.officeAddress == kwargs.get('officeAddress'))
                if kwargs.get('addTime'):
                    filter_list.append(cls.addTime == kwargs.get('addTime'))
                
            res = db.session.query(cls).filter(*filter_list).with_for_update()

            results = {
                'delete_time': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                'adminID': []
            }
            for query_model in res.all():
                results['adminID'].append(query_model.adminID)

            res.update({'isDelete': 1})
            db.session.commit()

            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'data': results}

        except Exception as e:
            db.session.rollback()
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'data': {'error': str(e)}}
        finally:
            db.session.close()
    
    # update
    @classmethod
    def update(cls, **kwargs):
        try:
            if kwargs.get('email'):
                kwargs['email'] = RSAEncryptionDecryption.encrypt(kwargs['email'])
            if kwargs.get('phone'):
                kwargs['phone'] = RSAEncryptionDecryption.encrypt(kwargs['phone'])
            
            filter_list = [cls.isDelete == 0]
            filter_list.append(cls.adminID == kwargs.get('adminID'))
            
            res = db.session.query(cls).filter(*filter_list).with_for_update()

            results = {
                'update_time': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                'adminID': res.first().adminID,
                
            }
            
            res.update(kwargs)
            db.session.commit()

            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'data': results}

        except Exception as e:
            db.session.rollback()
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'data': {'error': str(e)}}
        finally:
            db.session.close()

    # batch add
    @classmethod
    def add_list(cls, **kwargs):
        param_list = json.loads(kwargs.get('AdminList'))
        model_list = []
        for param_dict in param_list:
            from ..utils.generate_id import GenerateID
            adminID = GenerateID.create_random_id()
            
            model = Admin(
                adminID=adminID,
                userID=param_dict.get('userID'),
                departmentID=param_dict.get('departmentID'),
                name=param_dict.get('name'),
                email=RSAEncryptionDecryption.encrypt(param_dict.get('email')),
                phone=RSAEncryptionDecryption.encrypt(param_dict.get('phone')),
                officeAddress=param_dict.get('officeAddress'),
                addTime=param_dict.get('addTime'),
                
            )
            model_list.append(model)
        
        try:
            db.session.add_all(model_list)
            db.session.commit()
            results = {
                'added_records': [],
                'add_time': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            }
            for model in model_list:
                added_record = {}
                added_record['adminID'] = model.adminID
                
                results['added_records'].append(added_record)
            
            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'data': results}
            
        except Exception as e:
            db.session.rollback()
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'data': {'error': str(e)}}
        finally:
            db.session.close()
